﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Client.MyConst;

namespace Client.Managers
{
    class TTSManager
    {
        Dictionary<string, string> voiceSetting;
        string voiceName;
        string nameKey;
        VoiceArgs argsCurr;

        public TTSManager()
        {
            MyTTS.ttsInit();
            voiceSetting = Managers.Manager.settingM.voiceSetting;
            SetVoice(voiceSetting["voice"]);
        }


        public void Speak(string text)
        {
            Stop();
            if (text != null)
                MyTTS.ttsSpeak(text, 0);
        }

        public void Stop()
        {
            MyTTS.ttsStop();
        }

        public void Uninit()
        {
            MyTTS.ttsStop();
            MyTTS.ttsUnInit();
        }


        //=======================


        public string[] GetVoiceNames()
        {
            List<string> l = new List<string>();
            StringBuilder sb = new StringBuilder(100);
            int count = MyTTS.ttsGetVoiceCount();
            for(uint i = 0; i < count; i++)
            {
                int res = MyTTS.ttsGetVoiceNameByNumber(i, sb, 100);
                l.Add(sb.ToString());
                sb.Clear();
            }
            return l.ToArray();
        }

        public string GetVoiceName()
        {
            return voiceName;
        }
        public uint GetVoicePitch()
        {
            return argsCurr.pitch;
        }
        public uint GetVoiceSpeed()
        {
            return argsCurr.speed;
        }
        public uint GetVoiceVolume()
        {
            return argsCurr.volume;
        }

        public void SetVoice(string voiceName)
        {
            this.voiceName = voiceName;
            voiceSetting["voice"] = voiceName;
            //nameKey = voiceName.Replace(" ", "");
            string res;
            if (!voiceSetting.TryGetValue(voiceName, out res))
                res = voiceSetting["general"];
            var args = res.Split(',');
            uint speed = uint.Parse(args[0]);
            uint pitch = uint.Parse(args[1]);
            uint volume = uint.Parse(args[2]);
            argsCurr = new VoiceArgs(speed, pitch, volume);
            voiceSetting[voiceName] = argsCurr.GetArgsString();

            MyTTS.ttsSetVoiceString(voiceName);
            MyTTS.ttsSetSpeed(speed);
            MyTTS.ttsSetPitch(pitch);
            MyTTS.ttsSetVolume(volume);
        }
        public void SetVoiceSpeed(uint speed)
        {
            argsCurr.speed = speed;
            MyTTS.ttsSetSpeed(speed);
            voiceSetting[voiceName] = argsCurr.GetArgsString();
        }
        public void SetVoicePitch(uint pitch)
        {
            argsCurr.pitch = pitch;
            MyTTS.ttsSetPitch(pitch);
            voiceSetting[voiceName] = argsCurr.GetArgsString();
        }
        public void SetVoiceVolume(uint volume)
        {
            argsCurr.volume = volume;
            MyTTS.ttsSetVolume(volume);
            voiceSetting[voiceName] = argsCurr.GetArgsString();
        }


        //=========================

        class VoiceArgs
        {
            public uint speed { get;  set; }
            public uint pitch { get;  set; }
            public uint volume { get; set; }

            public VoiceArgs(uint speed, uint pitch, uint volume)
            {
                this.speed = speed;
                this.pitch = pitch;
                this.volume = volume;
            }

            public string GetArgsString()
            {
                return speed + "," + pitch + "," + volume;
            }
        }
    }

    class MyTTS
    {

        //初始化===========================

        [DllImport("TTS.dll")]
        public static extern int ttsInit();

        [DllImport("TTS.dll")]
        public static extern int ttsUnInit();


        //朗读====================================

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsSpeak(string lpStr, uint dwFlags);

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsStop();

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsPause();

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsResume();

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsIsSpeaking();


        //音库============================

        [DllImport("TTS.dll")]
        public static extern int ttsGetVoiceCount();

        [DllImport("TTS.dll")]
        public static extern int ttsSetVoice(uint dwIndex);

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsSetVoiceString(string lpszVoiceName);

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsGetVoiceNumber();

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsGetVoiceName(StringBuilder lpszVoiceName, uint dwSize);

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsGetVoiceNameByNumber(uint dwIndex, StringBuilder lpszVoiceName, uint dwSize);

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsGetVoiceNumberByName(string lpszVoiceName);

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsNextVoice(uint dwFlags);


        //速度===============================

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsSetSpeed(uint dwSpeed);

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsGetSpeed();

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsSpeedUp();

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsSpeedDown();


        //音高==================================

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsSetPitch(uint dwPitch);

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsGetPitch();

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsPitchUp();

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsPitchDown();


        //音量========================================

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsSetVolume(uint dwVolume);

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsGetVolume();

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsVolumeUp();

        [DllImport("TTS.dll", CharSet = CharSet.Unicode)]
        public static extern int ttsVolumeDown();
    }
}
